package com.onpositive.nlp.sequence;

import java.util.Collection;
import java.util.LinkedHashSet;

public class ClassifiedSequence {

	protected LinkedHashSet<Token> tokens = new LinkedHashSet<ClassifiedSequence.Token>();
	protected String original;
	protected Token pluralCore;
	protected String classificator;

	public String getClassificator() {
		return classificator;
	}

	protected boolean isClassification;
	protected boolean simpleClassification = true;
	protected Token classificationToken;
	private boolean isPlural;

	public boolean isPlural() {
		return isPlural;
	}

	public Token getCore() {
		return pluralCore;
	}

	public Token getClassificationCore() {
		return classificationToken;
	}

	public boolean isClassification() {
		return isClassification;
	}

	public boolean isSimpleClassification() {
		return simpleClassification;
	}

	public String getOriginal() {
		return original;
	}

	public void setOriginal(String original) {
		this.original = original;
	}

	public boolean add(Token e) {
		return tokens.add(e);
	}

	public boolean remove(Token o) {
		return tokens.remove(o);
	}

	public static class Token {
		public static final int REFERENCE = 1;
		public static final int CLASIFICATOR = 3;
		public static final int TEXT = 2;
		public static final int NUMBER = 4;
		public static final int NOUN = 4;
		public final String name;

		public final int kind;
		public boolean noun;
		public boolean verb;
		public boolean plural;

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + kind;
			result = prime * result + link;
			result = prime * result + ((name == null) ? 0 : name.hashCode());
			return result;
		}

		public String toString() {
			StringBuilder bl = new StringBuilder();
			if (kind == REFERENCE) {
				bl.append("ref-");
			}
			bl.append(name);
			if (noun) {
				bl.append("(N)");
			}
			if (plural) {
				bl.append("(P)");
			}
			return bl.toString();
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Token other = (Token) obj;
			if (kind != other.kind)
				return false;
			if (link != other.link)
				return false;
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equals(other.name))
				return false;
			return true;
		}

		public int link = -1;

		public Token(String name, int kind, int link) {
			super();
			this.name = name;
			this.kind = kind;
			this.link = link;
		}
	}

	public String toString() {
		StringBuilder bld = new StringBuilder();
		int z = 0;
		int sz = tokens.size();
		for (Token t : tokens) {
			bld.append(t);
			if (z != sz - 1) {
				bld.append('_');
			}
			z++;
		}
		return bld.toString();
	}

	public Collection<Token> tokens() {
		return tokens;
	}

	void lock() {
		Token lastNoun = null;
		for (Token t : tokens()) {
			if (t.name.equals("classifications")) {
				continue;
			}
			// if (t.kind == Token.REFERENCE) {
			if (isClassification) {
				if (classificationToken == null) {
					if (t.noun) {

						classificationToken = t;
						simpleClassification = true;
					} else {
						simpleClassification = false;
					}
				}
			} else if (t.noun) {
				if (true) {
					pluralCore = t;
					isPlural = true;
				}
			}
			if (t.kind == Token.CLASIFICATOR) {
				isClassification = true;
				if (classificator == null) {
					classificator = t.name;
				}
			} else {
				if (!isClassification) {
					if (t.name.endsWith("s") && t.kind == Token.REFERENCE) {
						pluralCore = t;
					}
					lastNoun = t;
				}
			}
		}
		if (pluralCore == null) {
			pluralCore = lastNoun;

		}
	}
}
